package com.nanbei.crmService.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nanbei.commonUntil.R;
import com.nanbei.crmService.entity.CrmBanner;
import com.nanbei.crmService.service.CrmBannerService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 首页banner表 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2022-01-17
 */
@RestController
@RequestMapping("/crmBanner/adminBanner")
@CrossOrigin
public class CrmAdminController {

    @Autowired
    private CrmBannerService crmBannerService;

    /**
     *分页查询轮播图
     * @param current
     * @param limit
     * @return
     */
    @ApiOperation(value = "分页查询轮播图）")
    @GetMapping("/pageListBanner/{current}/{limit}")
    public R pageListBanner(@PathVariable long current,
                             @PathVariable long limit){
        //创建page对象
        Page<CrmBanner> pageTeachers = new Page<>(current,limit);

        //调用分页方法
        //调用方法的时候，底层封装，把所有的数据封装到pageTeacher对象里面
        crmBannerService.page(pageTeachers, null);

        //返回总记录数
        long total = pageTeachers.getTotal();
        //数据List集合
        List<CrmBanner> records = pageTeachers.getRecords();

        return R.ok().data("total",total).data("rows",records);
    }

    /**
     * 添加轮播图
     * @param banner
     * @return
     */
    @PostMapping("/addBanner")
    public R addBanner(@RequestBody CrmBanner banner){
        crmBannerService.save(banner);
        return R.ok();
    }

    /**
     * 删除轮播图
     * @param bannerId
     * @return
     */
    @DeleteMapping("/delBannerById/{bannerId}")
    public R delBannerById(@PathVariable String bannerId){
        crmBannerService.removeById(bannerId);
        return R.ok();
    }

    /**
     * 修改轮播图
     * @return
     */
    @PostMapping("/updateBannerById/{bannerId}")
    public R updateBannerById(@RequestBody CrmBanner banner){
        crmBannerService.updateById(banner);
        return R.ok();
    }

    /**
     * 根据ID查询轮播图
     * @param bannerId
     * @return
     */
    @GetMapping("/findBannerById/{bannerId}")
    public R findBannerById(@PathVariable String bannerId){
        crmBannerService.getById(bannerId);
        return R.ok();
    }

}

